package com.manlitech.manlichat.service.nettywebsocket;

import com.manlitech.manlichat.base.exception.MyException;
import com.manlitech.manlichat.base.netty.message.container.MessageHandler;
import com.manlitech.manlichat.base.netty.pool.MyChannelHandlerPool;
import com.manlitech.manlichat.base.netty.util.MessageSendUtil;
import com.manlitech.manlichat.dao.ChatMessageDao;
import com.manlitech.manlichat.dao.ImUserDao;
import com.manlitech.manlichat.dao.ImUserFriendDao;
import com.manlitech.manlichat.dto.ChatMessage;
import com.manlitech.manlichat.dto.ImUser;
import com.manlitech.manlichat.dto.ImUserFriend;
import com.manlitech.manlichat.enums.ResultEnums;
import com.manlitech.manlichat.pojo.nettywebsocket.ChatToOneMessage;
import com.manlitech.manlichat.pojo.nettywebsocket.ImUserFriendMessage;
import com.manlitech.manlichat.service.ChatMessageService;
import com.manlitech.manlichat.util.ResultUtil;
import com.manlitech.manlichat.util.convert.ChatToOneMessageConvertToChatMessage;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ImUserFriendHandler implements MessageHandler<ImUserFriendMessage> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    MessageSendUtil messageSendUtil;

    @Autowired
    ChatMessageService chatMessageService;

    @Autowired
    ChatMessageDao chatMessageDao;

    @Autowired
    ImUserDao imUserDao;

    @Autowired
    ImUserFriendDao imUserFriendDao;


    @Override
    public void execute(Channel channel, ImUserFriendMessage message) throws Exception {
        //判断是否绑定
        ChannelId fromChannel = MyChannelHandlerPool.channelIdMap.get(message.getFromId());
        if(fromChannel != null){
            //将两人拉为好友 获取两人的im_user_id
            ImUser imUserFrom = imUserDao.createLambdaQuery().andEq("uniquekey",message.getFromId()).single();
            ImUser imUserTo = imUserDao.createLambdaQuery().andEq("uniquekey",message.getToId()).single();

            ImUserFriend sourceImUserFriend = imUserFriendDao.createLambdaQuery().andEq("im_user_id_from",imUserFrom.getId())
                    .andEq("im_user_id_to",imUserTo.getId()).single();
            if(sourceImUserFriend == null ){
                ImUserFriend imUserFriend = new ImUserFriend();
                imUserFriend.setImUserIdFrom(imUserFrom.getId());
                imUserFriend.setImUserIdTo(imUserTo.getId());
                imUserFriend.setStatus(1);
                imUserFriendDao.insert(imUserFriend);
                messageSendUtil.responseTheRequest(channel,ResultUtil.success("添加好友成功"));
            }else{
                messageSendUtil.responseTheRequest(channel,ResultUtil.success("好友已添加"));
            }
            return;
        }
        logger.error(ResultEnums.UN_AUTH.getMsg());
        //删除到channelGroup 通道组
        MyChannelHandlerPool.channelGroup.remove(channel);
        throw new MyException(ResultEnums.UN_AUTH);

    }

    @Override
    public String getType() {
        return ImUserFriendMessage.TYPE;
    }
}
